public class demo2 {
    public static void main(String[] args) {
        Object lock1 = new Object();
        Object lock2 = new Object();
        //标号: 锁1 为一号, 锁2 为二号. 由小到大加锁
        Thread t1 = new Thread(() -> {
            synchronized (lock1){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lock2){
                    System.out.println("获取锁2");
                }
            }
        });
        Thread t2 = new Thread(() -> {
            synchronized (lock1){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized ((lock2)){
                    System.out.println("获取锁1");
                }
            }
        });

        t1.start();
        t2.start();
    }
}
